package boardWindow;

import java.awt.Color;

import boardStructure.Board;


public class Refresher implements Runnable {

	private final int REFRESH_DELAY = 20;
	
	private Board board;
	private BoardPanel boardPanel;
	
	private int numOfCellsVertical;
	private int numOfCellsHorizontal;
	
	private Board tempBoard;
	

	public Refresher(Board _board, BoardPanel _boardPanel){
		board = _board;
		numOfCellsVertical = board.getNumOfCellsVertical();
		numOfCellsHorizontal = board.getNumOfCellsHorizontal();
		boardPanel = _boardPanel;
		
		tempBoard = new Board(board.getNumOfCellsHorizontal(),board.getNumOfCellsVertical());

	}
	@Override
	public void run() {
		
		while(true){
			tempBoard.copy(board);
			for( int i = 0 ; i < numOfCellsHorizontal; i++){
				for( int j = 0 ; j < numOfCellsVertical; j++){
			
					if (tempBoard.isCellAlive(i, j) == true){
						boardPanel.colorCellSquare(i, j, Color.BLUE);
						
					}
					else{
						boardPanel.colorCellSquare(i, j, Color.WHITE);
					}
				}
			}
			delay(REFRESH_DELAY);
			
		}
	}
	private void delay(int ms){
		try {
		    Thread.sleep(ms);
		} catch(InterruptedException ex) {
		    Thread.currentThread().interrupt();
		}
	}
}